package com.example.cr;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

/**
 * 内容解析方，增删改查4个按钮，是为了操作cp那个项目的数据
 * 实现步骤  --内容提供方
 * 1.创建自定义内容提供者-mycp.class
 * 2.重写里面5个方法 oncreate、增删改查
 * 3.在清单文件添加provider标签两个默认值添加上内容  自己增加exported enabled这两个属性
 * ---内容解析方
 * 1.根据实际情况实例化 内容解析者contentResolver 和  Uri路径
 * 2.执行增删改查方法
 *
 *
 *
 *
 *
 */
public class MainActivity extends AppCompatActivity {
    private ContentResolver cr;
    private Uri uri;//访问地址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cr = getContentResolver();
        //格式 content://+cp中清单文件中provider中的authorities的内容
        uri = Uri.parse("content://"+"com.bw.mycp");
    }

    public void query(View view) {
        Cursor cursor = cr.query(uri, null, null, null, null);
        if(cursor!=null){
            while (cursor.moveToNext()){
                @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
                @SuppressLint("Range") int age = cursor.getInt(cursor.getColumnIndex("age"));
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
                Log.d("sss", "query: "+id+","+name+","+age);
            }
        }
    }

    public void delete(View view) {
        cr.delete(uri,"name=?",new String[]{"张三"});
    }

    public void update(View view) {
        ContentValues values = new ContentValues();
        values.put("age",25);
        cr.update(uri,values,"name=?",new String[]{"张三"});
    }

    public void insert(View view) {
        ContentValues values = new ContentValues();
        values.put("name","张三");
        values.put("age",18);
        cr.insert(uri,values);
    }
}